package com.tinem.platform.module.starter.web.aop;

import com.tinem.platform.module.pojo.co.GatewayHeadName;
import com.tinem.platform.module.pojo.vo.error.SystemException;
import com.tinem.platform.module.starter.sdk.MessageCode;
import com.tinem.platform.module.starter.web.annotation.PagingApi;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * @author fengzhihao
 * @version v1
 * @program: platform
 * @className PageAop
 * @description TODO
 * @site 
 * @company 
 * @create 2020-07-14 22:03
 */
@Aspect
@Component
public class PageAop {
    /**
     * 分页方法拦截
     * @param jp
     * @return
     * @throws Throwable
     */
    @Around("@annotation(com.tinem.platform.module.starter.web.annotation.PagingApi)")
    public Object aroundLogger(ProceedingJoinPoint jp) throws Throwable {

        Signature signature = jp.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        PagingApi pagingApi = method.getDeclaredAnnotation(PagingApi.class);

        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = attributes.getResponse();
        if(jp.proceed() instanceof org.springframework.data.domain.Page){
            Page<?> page = (Page<?>) jp.proceed();
            response.setHeader(GatewayHeadName.X_PLATFORM_RES_PAGE_CURRENT, String.valueOf(page.getPageable().getPageNumber()));
            response.setHeader(GatewayHeadName.X_PLATFORM_RES_PAGE_SIZE, String.valueOf(page.getPageable().getPageSize()));
            response.setHeader(GatewayHeadName.X_PLATFORM_RES_PAGE_TOTAL, String.valueOf(page.getTotalElements()));
            response.setHeader(GatewayHeadName.X_PLATFORM_RES_PAGE_PAGES, String.valueOf(page.getTotalPages()));
            if (PagingApi.RES_TYPE_LIST.equals(pagingApi.type())) {
                return page.stream().toArray();
            }else {
                return page;
            }
        }else if(jp.proceed() instanceof com.github.pagehelper.Page){
            com.github.pagehelper.Page<?> page = (com.github.pagehelper.Page<?>) jp.proceed();
            response.setHeader(GatewayHeadName.X_PLATFORM_RES_PAGE_CURRENT, String.valueOf(page.getPageNum()-1));
            response.setHeader(GatewayHeadName.X_PLATFORM_RES_PAGE_SIZE, String.valueOf(page.getPageSize()));
            response.setHeader(GatewayHeadName.X_PLATFORM_RES_PAGE_TOTAL, String.valueOf(page.getTotal()));
            response.setHeader(GatewayHeadName.X_PLATFORM_RES_PAGE_PAGES, String.valueOf(page.getPages()));
            if (PagingApi.RES_TYPE_LIST.equals(pagingApi.type())) {
                return page.stream().toArray();
            }else {
                return page;
            }
        }else{
            throw new SystemException(MessageCode.ERROR_COMMONS_UNKNOWN,"分页注解返回的不是分页对象");
        }

    }
}
